//
//  NetWorTools+community.swift
//  FM
//
//  Created by x on 2017/10/13.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

extension NetWorTools {
    
    
    /// 加载社区数据
    ///
    /// - Parameters:
    ///   - type: 1表示精华  0表示最新
    ///   - offset: <#offset description#>
    ///   - successCallBack: <#successCallBack description#>
    ///   - errorCallBack: <#errorCallBack description#>
    func loadCommunityData(type: Int, offset:Int, successCallBack:@escaping (_ data:[Essence])->(), errorCallBack:@escaping (_ error:Error)->()) -> Void {
        let url = "http://yiapi.xinli001.com/fm/forum-thread-list.json"
        let param = ["type":type,"flag":0,"key":"c0d28ec0954084b4426223366293d190","size":10,"offset":offset] as [String : Any]
        get(url, parameters: param, progress: nil, success: { (_, JSON) in
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            guard data.count == 0 else {
                
                var model:[Essence] = [Essence]()
                
                for dic in data {
                    model.append(Essence(dic: dic))
                }
                successCallBack(model)
                return
            }
            
        }) { (_, error) in
            errorCallBack(error)
        }
    }
    
    /// 加载某条社区的评论
    ///
    /// - Parameters:
    ///   - post_id: <#post_id description#>
    ///   - offset: <#offset description#>
    ///   - successCallBack: <#successCallBack description#>
    ///   - errorCallBack: <#errorCallBack description#>
    func loadComments(post_id:String, offset:Int, successCallBack:@escaping (_ data:[EssenceComment])->(), errorCallBack:@escaping (_ error:Error)->()) -> Void {
        let url = "http://yiapi.xinli001.com/fm/forum-comment-list.json"//
        let param = ["key":"c0d28ec0954084b4426223366293d190","post_id":post_id,"offset":offset,"limit":10] as [String : Any]
        get(url, parameters: param, progress: nil, success: { (task, JSON) in
            //print("url=======",task.currentRequest?.url?.description)
            let data = (JSON as! [String:AnyObject])["data"] as! [[String:AnyObject]]
            guard data.count == 0 else {
                
                var model:[EssenceComment] = [EssenceComment]()
                
                for dic in data {
                    model.append(EssenceComment(dic: dic))
                }
                successCallBack(model)
                
                return
            }
        }) { (_, error) in
            
            errorCallBack(error)
        }
        
    }
}
